﻿#include <QApplication>
#include <QFileDialog>
#include <QtDebug>
#include <QMetaType>
#include "ui_process.h"
#include "file_process.h"
#include "track_process.h"
#include "../data_modules/track_data.h"

Ui_process::Ui_process(QObject *parent)
    : QObject(parent)
    , file_process_(new File_process())
    , track_process_(new Track_process())
{
    init_connects();
}

Ui_process::~Ui_process()
{
//    if ((nullptr != track_process_)
//            && (thread() == track_process_->thread())) {
//        delete track_process_;
//        track_process_ = nullptr;
//    }

    track_thread_.quit();
    track_thread_.wait();

//    if ((nullptr != file_process_)
//            && (thread() == file_process_->thread())) {
//        delete file_process_;
//        file_process_ = nullptr;
//    }

    file_thread_.quit();
    file_thread_.wait();
}

void Ui_process::get_filter_conditions(QMap<QString, QVariant> *filter, QList<Track_data *> *list)
{
    start_sub_thread(track_thread_, track_process_);

    emit si_send_filter_and_track_to_process(filter, list);
}

void Ui_process::sl_open_files()
{
    QStringList files_list = QFileDialog::getOpenFileNames();

    if ( !files_list.isEmpty()) {
        emit si_clear_model_();

        start_sub_thread(file_thread_, file_process_);

        emit si_open_files(files_list);
    }
}

void Ui_process::sl_split_data_to_files()
{
    QStringList files_list = QFileDialog::getOpenFileNames();

    if ( !files_list.isEmpty()) {
        start_sub_thread(file_thread_, file_process_);

        emit si_split_data_to_files(files_list);
    }
}

void Ui_process::sl_exit_application()
{
    QApplication::closeAllWindows();
}

void Ui_process::sl_quit_sub_thread(int index)
{
    switch (index) {
    case 0:
        if ( track_thread_.isRunning()) {
//            track_process_->moveToThread(this->thread());
//            track_thread_.quit();
        }
        break;

    case 1:
        if ( file_thread_.isRunning()) {
            file_thread_.quit();
            file_process_->moveToThread(this->thread());
//            file_process_->setParent(nullptr);
            disconnect(&file_thread_, nullptr, file_process_, nullptr);
        }
        break;
    }
}

void Ui_process::init_connects()
{
    connect(this, &Ui_process::si_split_data_to_files,
            file_process_, &File_process::sl_split_data_to_files);

    connect(this, &Ui_process::si_send_filter_and_track_to_process,
            track_process_, &Track_process::sl_filter_track_data);

    connect(this, &Ui_process::si_open_files,
            file_process_, &File_process::sl_open_files_to_show);

    connect(file_process_, &File_process::si_send_track_to_model,
            this, &Ui_process::si_send_track_to_model);

    connect(track_process_, &Track_process::si_update_track_by_filter,
            this, &Ui_process::si_updte_model_by_filter);

    // 剔除删除子线程信号槽
//    connect(file_process_, &File_process::si_quit_sub_thread,
//            this, &Ui_process::sl_quit_sub_thread);
}

void Ui_process::start_sub_thread(QThread &thread, QObject *process)
{
    if ( !thread.isRunning()) {
        if (nullptr != process) {
            // Move object into sub thread. create signal/slot
            process->moveToThread(&thread);
            connect(&thread, &QThread::finished, process, &QObject::deleteLater);

            thread.start();
        }
    }
}

